package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemProcessRecordTemplateInfoDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.entity.ItemProcessRecordTemplateInfo;
import com.jyu.apts.entity.InfoTemplate;
import com.jyu.apts.entity.ItemProcessRecord;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.service.ItemProcessRecordTemplateInfoService;
import com.jyu.apts.service.InfoTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class ItemProcessRecordTemplateInfoController {

	@Autowired
	InfoTemplateService infoTemplateService;
	@Autowired
	UserService userService;
	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	@Autowired
	ItemProcessRecordTemplateInfoService itemProcessRecordTemplateInfoService;
	
	@RequiresPermissions("itemProcessRecordTemplateInfo_html")
	@RequestMapping("/itemProcessRecordTemplateInfo_html")
	public ModelAndView itemProcessRecordTemplateInfo_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/itemProcessRecordTemplateInfo.html");
		return mv;
	}
	
	@RequiresPermissions("itemProcessRecordTemplateInfo_html")
	@RequestMapping("/addItemProcessRecordTemplateInfo")
	public ResultDto addItemProcessRecordTemplateInfo(String[] dataValues,long infoTemplateId,long itemProcessRecordId) {
		InfoTemplate infoTemplate = infoTemplateService.getOne(infoTemplateId);
		if(infoTemplate == null) {
			return new ResultDto("信息模板不存在",null,2);
		}
		ItemProcessRecord itemProcessRecord = itemProcessRecordService.getOne(itemProcessRecordId);
		ItemProcessRecordTemplateInfo itemProcessRecordTemplateInfo = new ItemProcessRecordTemplateInfo();
		StringBuilder sb = new StringBuilder("");
		if(dataValues == null || dataValues.length == 0) {
			sb.append("#");
		}
		for(String dataValue : dataValues) {
			sb.append("#");
			sb.append(dataValue);
		}
		itemProcessRecordTemplateInfo.setDataValues(sb.toString().substring(1));
		itemProcessRecordTemplateInfo.setInfoTemplate(infoTemplate);
		itemProcessRecordTemplateInfo.setItemProcessRecord(itemProcessRecord);
		itemProcessRecordTemplateInfoService.save(itemProcessRecordTemplateInfo);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("itemProcessRecordTemplateInfo_html")
	@RequestMapping("/delItemProcessRecordTemplateInfo")
	public ResultDto delItemProcessRecordTemplateInfo(String ids) {
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : ids.split(",")) {
			l_ids.add(new Long(id));
		}
		List<ItemProcessRecordTemplateInfo> itemProcessRecordTemplateInfos = itemProcessRecordTemplateInfoService.findAllById(l_ids);
		itemProcessRecordTemplateInfoService.delete(itemProcessRecordTemplateInfos);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("itemProcessRecordTemplateInfo_html")
	@RequestMapping("/getItemProcessRecordTemplateInfoPage")
	public TableDto<ItemProcessRecordTemplateInfoDto> getItemProcessRecordTemplateInfoPage(Integer pageNumber,Integer pageSize,Integer status,String search,Long organizationId) {
		Page<ItemProcessRecordTemplateInfo> pageResult = itemProcessRecordTemplateInfoService.getItemProcessRecordTemplateInfoPage(pageNumber, pageSize, search, organizationId, null);
		List<ItemProcessRecordTemplateInfo> itemProcessRecordTemplateInfos = pageResult.getContent();
		List<ItemProcessRecordTemplateInfoDto> itemProcessRecordTemplateInfoDtos = new ArrayList<ItemProcessRecordTemplateInfoDto>();
		itemProcessRecordTemplateInfos.forEach(itemProcessRecordTemplateInfo->itemProcessRecordTemplateInfoDtos.add(new ItemProcessRecordTemplateInfoDto().convert(itemProcessRecordTemplateInfo)));
		return new TableDto<ItemProcessRecordTemplateInfoDto>(itemProcessRecordTemplateInfoDtos,pageResult.getTotalElements());
	}
	
}
